package com.sgu6.sgu6_machine.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.middleware.util.HexUtil;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.common.Constant;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.presenter.MachineP;


/**
 * 包括自动开关机,自动刷新广告
 */
public class AppR extends BroadcastReceiver {

    private static final String TAG = AppR.class.getSimpleName();
    private MachineP machineP = new MachineP();

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Constant.ACTION_ALARM_ON.equals(action)) {
            Log.i(TAG, "onReceive: 开");
            doAlarmOnAction();
        } else if (Constant.ACTION_ALARM_OFF.equals(action)) {
            Log.i(TAG, "onReceive: 关");
            doAlarmOffAction();
        } else if (Constant.ACTION_AD_REFRESH.equals(action)) {
            doRefreshAdAction();
        }
    }

    public void doRefreshAdAction() {
        Log.i(TAG, "doRefreshAdAction: ");
        machineP.getSysNoticeByType(1);
    }

    public void doAlarmOnAction() {
        String h = String.format(MachineCmdEnum.设置横封温度.code(), 1200);
        SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(h));
        String v = String.format(MachineCmdEnum.设置纵封温度.code(), 1200);
        SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(v));
    }


    public void doAlarmOffAction() {
        String h = String.format(MachineCmdEnum.设置横封温度.code(), 200);
        SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(h));
        String v = String.format(MachineCmdEnum.设置纵封温度.code(), 200);
        SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(v));
    }

}
